/**
 * @author yinyuejia
 * @file 系统相关方法
 */

import store from '@/store';
import config from '@/config';
import router from '@/router';
import { defaultRoutes } from '@/router/constantRoute';

// 递归查询菜单有无子元素
function hasChild(name: String, menuList: Array<any>, parentList: Array<any>) {
  return menuList.some((menu: any) => {
    if (menu.code === name) {
      return true;
    }
    if (menu.children && menu.children.length > 0) {
      if (hasChild(name, menu.children, parentList)) {
        parentList.push(menu);
        return true;
      }
    }
    return false;
  });
}

// 设置面包屑路径
function setBreadCrumb(name: String, to: { meta: { menu: String; title: String }; name: String }) {
  const menuList = store.getters.leftMenuList;

  if (menuList.length > 0) {
    const parentList: Array<any> = [];
    if (hasChild(name, menuList, parentList)) {
      const newList: Array<any> =
        parentList.length > 0
          ? parentList.reverse().map((item: any) => ({
              name: item.code,
              title: item.name,
              url: item.url,
            }))
          : [];
      if (to.meta.menu) {
        const route = router.getRoutes().find((item: any) => item.name === to.meta.menu);
        if (route) {
          newList.push({
            name: route.meta.menu,
            title: route.meta.title,
            url: route.path,
          });
        }
      }
      newList.push({
        name: to.name,
        title: to.meta.title,
      });
      store.commit('setBreadCrumb', { breadcrumb: newList });
    }
  }
}

function setTag(to: any) {
  if (!config.system.SHOW_TAG) {
    return;
  }
  if (!(['Login', '404'].indexOf(to.name) === -1 && (to.name || to.meta.title))) {
    return;
  }
  let targetTagCode = to.name;
  const showTag = to.meta.tag !== false;
  let tagItem: any = {
    name: targetTagCode,
    title: to.meta.title,
    params: to.params,
    query: to.query,
  };
  if (!showTag) {
    // 使用所属菜单的tag
    if (to.meta.menu) {
      const route = router.getRoutes().find((a: any) => a.name === to.meta.menu);
      if (route) {
        targetTagCode = to.meta.menu;
        tagItem = {
          name: to.meta.menu,
          title: route.meta.title,
        };
      }
    }
  }
  // @ts-ignore
  const originTagCode = store.state.menu.activeTag;
  if (targetTagCode === originTagCode) {
    return;
  }
  // @ts-ignore
  const tagList = store.state.menu.tagList || [];
  if (!tagList.some((a: any) => a.name === 'Home')) {
    const homeRoute: any = defaultRoutes.find((a: any) => a.name === 'Home');
    const item = {
      name: homeRoute.name,
      title: homeRoute.meta.title,
    };
    store.commit('addTag', item);
  }
  if (!tagList.some((a: any) => a.name === targetTagCode)) {
    store.commit('addTag', tagItem);
  }
  store.commit('setActiveTag', targetTagCode);
}
function setCache(to: any) {
  const targetTagCode = to.name;
  if (to.meta.cache === true || to.meta.cache === 'true') {
    store.commit('addCacheRoute', targetTagCode);
    // 不显示tag，包含在已有菜单的tag中
    if (to.meta.tag === false && to.meta.menu) {
      const route = router.getRoutes().find((a: any) => a.name === to.meta.menu);
      if (route) {
        store.commit('setTagChildren', {
          key: to.meta.menu,
          value: targetTagCode,
        });
      }
    }
  }
}
export default {
  routerFinished: (to: any) => {
    // 菜单open状态
    let targetIndex = to.name;
    if (to.meta.menu) {
      targetIndex = to.meta.menu;
    }
    store.commit('setActiveLeftMenu', targetIndex);
    // breadcrumb
    setBreadCrumb(targetIndex, to);
    // 设置tag标签
    setTag(to);
    // keep-alive缓存
    setCache(to);
  },
};
